package com.badibadi.uniclubber;

import java.util.ArrayList;
import java.util.List;

import com.badibadi.infos.SinaSource;
import com.badibadi.infos.SinaSourceModel;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.SinaExpressionTools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.MyGridView_ShowAll;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 新浪表情： 创建时间 2014-10-14
 * 
 * @author Administrator result回传标号
 */
public class SinaExpressionActivity extends Activity {
	public static final int ACTIVITY_BIAOQING = 8081;// 回传的资源号数
	ViewPager viewPager;
	List<View> gridViewList; // 裝載gridview的容器
	List<SinaSourceModel> listInfo; // 总数据
	int page;// viewpager的頁數
	Context context = SinaExpressionActivity.this;
	SinaSource sina = new SinaSource();
	private DisplayImageOptions options;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sina_expression);

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
		// 裝載所有顯示數據得容器
		listInfo = new ArrayList<SinaSourceModel>();
		// 裝載gridview的容器
		gridViewList = new ArrayList<View>();
		listInfo = sina.getSinaSource();
		// 获取所有数据的总长
//		int size = listInfo.size();
		int size=60;
		if (size == 0)
			return;

		// 確定viewpager的頁數
		int len = size / 21;
		if (size % 21 == 0) {
			page = len;
		} else {
			page = len + 1;
		}

		// 為viewpager裝配數據
		viewPager = (ViewPager) findViewById(R.id.viewpagerLayout);
		viewPager.setAdapter(new MyPagerAdapter());
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int pages) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int page) {
			}
		});
	}

	/**
	 * viewpager的數據
	 * 
	 * @author Administrator
	 * 
	 */
	class MyPagerAdapter extends PagerAdapter {
		public MyPagerAdapter() {
			// viewpage所需要的界面
			for (int i = 0; i < page; i++) {
				// 第三步：使用gridview来装已安装的apk
				GridView gridView = new MyGridView_ShowAll(context);
				gridView.setNumColumns(7);
				gridView.setGravity(Gravity.BOTTOM);
				gridView.setAdapter(new MyListAdapter(i));

				gridViewList.add(gridView);
			}
		}

		@Override
		public int getCount() {
			return page;// 总页数
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = gridViewList.get(position);
			container.addView(view);
			return view;
		}

	}

	/**
	 * gridview的數據
	 * 
	 * @author Administrator
	 * 
	 */
	public class MyListAdapter extends BaseAdapter {

		ArrayList<SinaSourceModel> gridInfo;

		public MyListAdapter(int page) {
			gridInfo = new ArrayList<SinaSourceModel>();
			for (int i = page * 21; i < page * 21 + 21; i++) {
				if (i == listInfo.size())
					break;
				gridInfo.add(listInfo.get(i));
			}
		}

		@Override
		public int getCount() {
			return gridInfo == null ? 0 : gridInfo.size();
		}

		@Override
		public Object getItem(int position) {
			return gridInfo == null ? null : gridInfo.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		ViewHolder holder = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// 1、如果没有可利用item时，找出所有控件
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.gridview_item, parent, false);
				holder.iconiView = (ImageView) convertView
						.findViewById(R.id.image);
				// holder.textView = (TextView)
				// convertView.findViewById(R.id.text);
				// 2、绑定
				convertView.setTag(holder);
			} else {// 3、有可利用的item时就获取赋值使用
				holder = (ViewHolder) convertView.getTag();
			}

			// 4、赋值
			final String info = gridInfo.get(position).getPath();
			final String info3 = gridInfo.get(position).getReplace();// 这是path替换过后的
			// 对sina表情进行一次截取处理
			String info2 = SinaExpressionTools.toImgPath(info);
//			ImageLoader.getInstance().displayImage(info2, holder.iconiView,
//					options);
			
			new BitmapHelp().displayImage(context, holder.iconiView, info2);
			
			
			

			// holder.iconiView.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
			// holder.textView.setText(info.activityInfo.loadLabel(getPackageManager()));//获取应用名
			// 监听点击事件
			holder.iconiView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					Intent intent = new Intent();
					intent.putExtra("result", info3);
					setResult(ACTIVITY_BIAOQING, intent);
					finish();

					// String info = gridInfo.get(position);
					// //获取包名
					// String packageName = info.activityInfo.packageName;
					// //获取activity类
					// String activityName = info.activityInfo.name;
					// //根据包名和activity来启动程序
					// ComponentName component = new ComponentName(packageName,
					// activityName);
					// Intent intent = new Intent();
					// intent.setComponent(component);
					// startActivity(intent);
				}
			});
			return convertView;
		}

		private class ViewHolder {
			ImageView iconiView;
			TextView textView;
		}
	}

	/**
	 * 第二部：获取所有安装的apk
	 */
	public List<ResolveInfo> getInstallAPK() {
		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		return getPackageManager().queryIntentActivities(intent, 0);
	}
}
